home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2748 < prev    next >
Encoding:
Text File  |  1996-08-06  |  5.2 KB  |  110 lines

  1. Newsgroups: comp.lang.c++,comp.programming
  2. Path: uu4news.netcom.com!friend!news
  3. From: rich@kastle.com (Richard Krehbiel)
  4. Subject: Re: Why are 32 bit better than 16 bit pgms?
  5. Message-ID: <1996Jan19.130447.12215@friend.kastle.com>
  6. Sender: news@friend.kastle.com (News)
  7. Reply-To: rich@kastle.com
  8. Organization: Kastle Development Associates
  9. X-Newsreader: Forte Free Agent 1.0.82
  10. References: <30FBFFE6.1FEB@netcom.com>
  11. Date: Fri, 19 Jan 1996 13:03:41 GMT
  12.  
  13. "Keith S." <vain@netcom.com> wrote:
  14.  
  15. >I have a simple questions:
  16.  
  17. >   What's are 32 bit pgms better than 16 bit programs?
  18.  
  19. >   Thanks!
  20.  
  21. Well, I have two 16-bit contexts I can relate to: PDP-11 and Intel.
  22.  
  23. The PDP-11 is truly a 16 bit CPU.  It has a 64K address space, period.
  24. Going to 32 bits means being able to use more than 64K.  Simple enough
  25. to understand, eh?  (Well... PDP-11's usually have a protected MMU,
  26. only visible/useable by the OS, that can address up to 4M, and usually
  27. OS offers services to "bank-swap" parts of the app's memory space.)
  28.  
  29. The Intel is a little tricker.  Simple answer: Addressing large memory
  30. is *much* faster in 32 bits than in 16 bits, which must deal with
  31. things as if it were multiple 64K pieces.  Read on for details...
  32.  
  33. It was born with segment:offset addressing, where a pointer to memory
  34. has two 16 bit parts.  Your instructions run faster when you only
  35. consider and manipulate the 16 bit offset part.  This is called a
  36. "near pointer", when only the 16 bit offset is known and the segment
  37. part is "assumed".  To increment a near pointer is one fast
  38. instruction.  Of course, since a near pointer is only 16 bits, there
  39. can only be 64K of "near" memory - not enough.
  40.  
  41. If you want to deal with memory objects larger than 64K you have to
  42. use a "far" pointer, which includes both the 16 bit segment and 16 bit
  43. offset.  To increment a far pointer, you increment the offset until it
  44. overflows, then you change the segment.  This is *far* slower.  First
  45. of all you have to look for overflow from the inc, meaning a
  46. conditional branch, the kind of thing that defeats instruction
  47. prefetch and pipelines.  And when I say "change the segment" I'm
  48. talking about a serious operation.  First of all, the segment part in
  49. a protected mode program is an MMU table subscript.  The MMU owner
  50. (the OS) defines what segment points to what memory and what kind of
  51. memory it is.  You can't just inc the segment value (or add 0x100 like
  52. in real mode) to address the next consecutive memory location after an
  53. offset overflow, you have to know the OS's convention for allocating
  54. segment values.
  55.  
  56. Now add to that the fact that the simple-looking 16 bit segment load
  57. operation invokes complicated processor behavior.  Remembering that a
  58. segment value is really an MMU table subscript, when you load the
  59. segment register, behind the scenes the CPU checks the value against
  60. the LDT/GDT to see if it's legal, then it fetches the 8 byte MMU table
  61. entry into a segment cache, and while it does it performs some other
  62. validity checks as well.  The result is that segment register loading
  63. is a SLOW instruction.
  64.  
  65. My 386 reference says moving a register to another register takes two
  66. clock cycles, unless the destination is a segment register, in which
  67. case it takes 18.  It takes nine times as long.  Sheesh.  A "near"
  68. subroutine call takes 7 cycles.  A far subroutine call takes 34 -
  69. about five times slower.  I don't have any newer references; perhaps
  70. the differences are smaller in the Pentium.
  71.  
  72. Now for 32 bit mode:
  73.  
  74. In 32 bit mode, suddenly the offsets are 32 bits.  Now a "near"
  75. pointer (where the segment part is just assumed) is 32 bits and is
  76. large enough to address all the memory.  Hey, the segment registers
  77. are still there and still fully functional; the 386 can support
  78. multiple 4G segments.  However, ALL the 32 bit OS designers said "f*ck
  79. that" and gave applications only a single segment of enormous size.
  80.  
  81. Suddenly life is good.  Pointer math for large objects is of the
  82. simple one-instruction kind, as fast as any other math.  Pointer
  83. loading is as fast as any other 32 bit load.  No more segment override
  84. prefixes.  In 16 bit mode it's architecturally impossible to have a
  85. stack larger than 64K, but in 32 bit mode it's as large as, well,
  86. every other segment (there's but one, remember?).  Things are simpler
  87. and faster, and you can (almost) forget that the abomination of
  88. segments ever existed.
  89.  
  90. On the other hand... Suppose you have a program that's small, simple
  91. and fast, it never needed more that 64K of memory and it doesn't count
  92. higher than 64 thousand.  32 bits won't make it faster, in fact it'll
  93. make it larger and slower.  Sorry.
  94.  
  95. Oh, let me just throw in the newest good reason why 32 bits is faster
  96. than 16 bits.  The new Pentium Pro slows down on 16 bit code.  It uses
  97. fascinating new technology, replete with all the latest buzzwords; x86
  98. instructions are decomposed into nano-ops and scheduled out-of-order
  99. to multiple functional units.  There's a little problem with it,
  100. however.  It's fancy data paths are designed for carrying 32 bit
  101. values around.  If forced to operate on pieces of whole registers,
  102. like it would in 16 bit mode, then it's registers can't rename and
  103. it's pipelines stall until it can make whole 32 bit results from 16
  104. bit operations.
  105.  
  106. --
  107. Richard Krehbiel, Kastle Systems, Arlington VA USA
  108. rich@kastle.com (work) or richk@mnsinc.com (personal)
  109.  
  110.